import {
  SAVE_BANNER,
  SAVE_HOTRECOMMENDS,
  SAVE_NEWALBUMS,
  SAVE_UPRANKING,
  SAVE_NEWRANKING,
  SAVE_ORIGINRANKING
} from './constant.js'

import {
  getBanner,
  getHotRecommends,
  getNewAlbums,
  getTopList

} from '@/network/recommend'




export const bannerAction = banner => ({
  type: SAVE_BANNER,
  banner
})

export const hotRecommendAction = hotRecommends => ({
  type: SAVE_HOTRECOMMENDS,
  hotRecommends
})

export const NewAlbumsAction = newAlbums => ({
  type: SAVE_NEWALBUMS,
  newAlbums
})

export const upRankingAction = upRanking => ({
  type: SAVE_UPRANKING,
  upRanking
})
export const newRankingAction = newRanking => ({
  type: SAVE_NEWRANKING,
  newRanking
})
export const originRankingAction = originRanking => ({
  type: SAVE_ORIGINRANKING,
  originRanking
})


// 利用redux-thunk库 可以在dispatch函数中传入一个函数，并会自动调用函数，在函数里再dispatch('传一个对象/调用返回对象的函数')，以及发异步请求
export const getBannerAction = async (dispatch) => {
  const banner = await getBanner()
  dispatch(bannerAction(banner.banners))
}


export const getHotRecommendAction = (limit) => {
  return async (dispatch) => {
    const hotRecommends = await getHotRecommends(limit)
    dispatch(hotRecommendAction(hotRecommends.result))
  }
}

export const getNewAlbumsAction = (limit) => {
  return async (dispatch) => {
    const newAlbums = await getNewAlbums(limit)
    dispatch(NewAlbumsAction(newAlbums.albums))
  }
}

export const getTopListAction = (idx) => {
  return async (dispatch) => {
    const ranking = await getTopList(idx)

    switch (idx) {
      case 0:
        dispatch(upRankingAction(ranking.playlist))
        break;
      case 2:
        dispatch(newRankingAction(ranking.playlist))
        break;
      case 3:
        dispatch(originRankingAction(ranking.playlist))
        break;
      default:
        break;
    }
  }
}